$(function() {
    $("#school_all").click(function (){
        let serverUrl = "http://localhost:8082/school/all";
        $.ajax({
            headers: {
                "X-XSRF-TOKEN": $.cookie("XSRF-TOKEN")
            },
            data:"",
            type:"GET",
            dataType:"json",
            async:true,
            crossDomain: true,
            contentType: "application/json",
            url:serverUrl,
            success:function(data){
                if(data.code == "10000"){
                    window.location.href = "login.html";
                }else{
                    createTable(data.data);
                }
            }
        });
    });

    function createTable(data) {
        var _html = "";
        for(var i in data){
            var school = data[i];
            _html += "<tr><td>" + school.name + "</td></tr>";
        }
        $("#school").html(_html);
    }

    $("#school_id").click(function (){
        let serverUrl = "http://localhost:8082/school/detail?id=1";
        $.ajax({
            headers: {
                "X-XSRF-TOKEN": $.cookie("XSRF-TOKEN")
            },
            data:"",
            type:"GET",
            dataType:"json",
            async:true,
            crossDomain: true,
            contentType: "application/json",
            url:serverUrl,
            success:function(data){
                if(data.code == "10000"){
                    window.location.href = "login.html";
                }else{
                    createTable(new Array(data.data));
                }
            }
        });
    });

    $("#admin_users").click(function (){
        let serverUrl = "http://localhost:8082/admin/all";
        $.ajax({
            headers: {
                "X-XSRF-TOKEN": $.cookie("XSRF-TOKEN")
            },
            data:"",
            type:"GET",
            dataType:"json",
            async:true,
            crossDomain: true,
            contentType: "application/json",
            url:serverUrl,
            success:function(data){
                if(data.code == "20000"){ // 没有权限
                    alert(data.message);
                }else if(data.code == "10000"){ // 没有认证
                    window.location.href = "login.html";
                }
                else{
                    createTable(data.data);
                }
            }
        });
    });

});